#!/usr/bin/env node

const https = require('https');

// --- 配置 ---

// 从 cURL 命令中提取的请求头
const headers = {
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'ajax': 'json',
    'content-type': 'application/json',
    'origin': 'http://yingyu00e-t.gsudev.cn',
    'priority': 'u=1, i',
    'referer': 'http://yingyu00e-t.gsudev.cn/',
    'rtp': '1',
    'sec-ch-ua': '"Google Chrome";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'cross-site',
    'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1IjoiMTEwMjk3OmFnZW5jeWEyMWQzYmZmNWQ3YjQ5NjM5NTQxOGEyOWExMGZjODgxIn0.WezW4RkNEv1RtsLlC_tFFMnQgcWOrj24ENNeLoBm_SI',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'
};

// 从 cURL 命令中提取的请求体
const postData = JSON.stringify({
    "student": {
        "id": 180,
        "name": "赵星明",
        "sn": "229812479554865",
        "grade": "一年级",
        "school": ""
    },
    "mobile": "15810275844",
    "grade": "一年级",
    "examTotalScore": "",
    "expectStartTime": "",
    "tutored": 0,
    "studentDescription": "",
    "courseSuggestion": "",
    "reviewSuggestion": "",
    "studentId": 180,
    "examType": "",
    "examScore": ""
});

const options = {
    hostname: 'qingqiu-api.gsudev.cn',
    path: '/api/english/japi/exp-receipt/add',
    method: 'POST',
    headers: headers
};

// --- 脚本逻辑 ---

// 解析命令行参数
const args = process.argv.slice(2);
const interval = parseInt(args[0], 10) || 3000; // 默认间隔1000ms
const totalCalls = parseInt(args[1], 10) || 100;   // 默认调用5次

let callCount = 0;

console.log(`开始执行脚本: 每 ${interval / 1000} 秒调用一次接口，共调用 ${totalCalls} 次。`);
console.log('--------------------------------------------------');

// API 调用函数
function callApi() {
    const req = https.request(options, (res) => {
        let data = '';
        console.log(`状态码: ${res.statusCode}`);

        res.on('data', (chunk) => {
            data += chunk;
        });

        res.on('end', () => {
            console.log('响应内容:', data);
            console.log('--------------------------------------------------');
        });
    });

    req.on('error', (e) => {
        console.error(`请求遇到问题: ${e.message}`);
    });

    // 写入数据到请求主体
    req.write(postData);
    req.end();
}

// 设置定时器
const intervalId = setInterval(() => {
    if (callCount >= totalCalls) {
        clearInterval(intervalId);
        console.log('所有调用执行完毕。');
        return;
    }

    console.log(`第 ${callCount + 1} 次调用...`);
    callApi();
    callCount++;

}, interval);

// 使用说明
if (args.length === 0) {
    console.log(`
用法: node ${process.argv[1]} [间隔毫秒数] [总调用次数]
示例: node ${process.argv[1]} 2000 10  (每2秒调用一次，共10次)
默认: 每1秒调用一次，共5次
    `);
}
